import api from "../../utils/api";
const app = getApp()

// pages/formHome/formHome.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 类型 1 临时门禁卡, 2 来访申请, 3 监控调取, 4 保持开机
    formCardList: [
      {text: '外来人员', src: '/images/formhome_card1.png'},
      {text: '门禁卡', src: '/images/formhome_card2.png'},
      {text: '监控调取', src: '/images/formhome_card3.png'},
      {text: '保持开机', src: '/images/formhome_card4.png'},
    ],
    formInitalHeader: [
      [
        {
          text: '未提交',
          applyStatus: 0,
          num: 0,
        },
        {
          text: '审批中',
          applyStatus: 1,
          num: 0,
        },
        {
          text: '已完成',
          applyStatus: 2,
          num: 0,
        }
      ],
      [
        {
          text: '待审批',
          applyStatus: 1,
          num: 0,
        },
        {
          text: '已审批',
          applyStatus: 2,
          num: 0,
        }
      ],
    ],
    formHeader: [],
    formHeaderIndex: 0,
    tipsDialog: false,
    pageIndex: 1,
    pageSize: 20,
    maxPages: null,
    requestList: [],
    approRole: "",
    unreadNum: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let approRole = wx.getStorageSync("logindata").data.approRole
    if (!approRole) {
      this.setData({
        formHeader: this.data.formInitalHeader[0],
        approRole: '',
      })
    } else {
      this.setData({
        formHeader: this.data.formInitalHeader[1],
        approRole: approRole
      })
    }
  },

  handleFormType: function (e) {
    this.setData({
      formHeaderIndex: e.currentTarget.dataset.index
    })
  },
  downLoadTips: function () {
    wx.downloadFile({
      url: 'https://bc-test.542124141.xyz/wx_file/关于我司员工前往深圳分中心开展工作的说明.docx',
      success (res) {
        if (res.statusCode === 200) {
          wx.openDocument({
            filePath: res.tempFilePath,
            showMenu: true,
            success: function (res) {
              // console.log('打开成功，转发即可')
            }
          })
        }
      }
    })
  },
  handleTipsDialog: function (e) {
    let flag = e.currentTarget.dataset.flag
    this.setData({
      tipsDialog: flag == 'true' ? true : false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      pageIndex: 1,
      maxPages: null,
      requestList: []
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('到底了')
    if (this.data.maxPages > this.data.pageIndex) {
      let pageIndex = this.data.pageIndex + 1
      this.setData({
        pageIndex
      })
      this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  handleFormCard: function (e) {
    switch (e.currentTarget.dataset.index){
      case 0:
        this.goToTempPass()
        break;
      case 1:
        this.goToKeycard()
        break;
      case 2:
        this.goToMonitor()
        break;
      case 3:
        this.goToPowerOn()
        break;
      default:
        break;
    }
  },

  goToTempPass: function (e) {
    this.setData({
      tipsDialog: false
    })
    wx.navigateTo({
      url: "/pages/formTempPass/formTempPass"
    })
  },

  goToKeycard: function (e) {
    this.setData({
      tipsDialog: false
    })
    wx.navigateTo({
      url: "/pages/formKeycard/formKeycard"
    })
  },

  goToMonitor: function (e) {
    this.setData({
      tipsDialog: false
    })
    wx.navigateTo({
      url: "/pages/formMonitor/formMonitor"
    })
  },

  goToPowerOn: function (e) {
    this.setData({
      tipsDialog: false
    })
    wx.navigateTo({
      url: "/pages/formPowerOn/formPowerOn"
    })
  },

  getList: function (e) {
    if (e) {
      this.setData({
        formHeaderIndex: e.currentTarget.dataset.index,
        pageIndex: 1,
        requestList: []
      })
    }

    let _this = this
    let url = this.data.approRole == '' ? api.formGetList : api.formGetListForApprover
    let applyStatus = this.data.approRole == '' ? this.data.formHeaderIndex : this.data.formHeaderIndex + 1
    wx.request({
      url: url,
      method: "GET",
      data: {
        workno: wx.getStorageSync("workNo"),
        // workno: 2975,
        applyStatus: applyStatus,
        pageIndex: this.data.pageIndex,
        pageSize: this.data.pageSize
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        if (res.data.state == 1) {
          let requestList = _this.data.requestList.concat(res.data.data.list)
          let maxPages = res.data.data.pages
          _this.setData({
            // requestList: res.data.data.list,
            maxPages,
            requestList
          })
        }
      }
    })
    this.getUnreadNum()
  },

  delRequest: function (e) {
    let id = e.target.dataset.id
    let _this = this
    wx.request({
      url: api.formDelRequest,
      method: "POST",
      data: {
        workno: wx.getStorageSync("workNo"),
        id: id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.state == 1) {
          wx.showToast({
            icon: 'success',
            title: "删除成功",
          })
          _this.getList()
        }
      }
    })
  },

  goToDetail: function (e) {
    let id = e.target.dataset.id
    let approRole = this.data.approRole
    let state = this.data.formHeaderIndex
    let type = e.target.dataset.type

    if (type == 1 || type == 2) {
      if (state == 0 && this.data.approRole == '') {
        // 未提交
        let url = type == 2 ? '/pages/formTempPass/formTempPass?id=' + id : '/pages/formKeycard/formKeycard?id=' + id
        wx.navigateTo({
          url: url,
        })
      } else {
        // 已提交
        wx.navigateTo({
          url: '/pages/formApprove/formApprove?id=' + id + '&approRole=' + approRole,
        })
      }
    } else {
      switch (type){
        case 3:
          wx.navigateTo({
            url: '/pages/formMonitor/formMonitor?id=' + id + '&approRole=' + approRole,
          })
          break;
        case 4:
          wx.navigateTo({
            url: '/pages/formPowerOn/formPowerOn?id=' + id + '&approRole=' + approRole,
          })
          break;
        default:
          break;
      }
    }
  },

  getUnreadNum: function () {
    let _this = this
    wx.request({
      url: api.formGetUnreadCount,
      method: "GET",
      data: {
        workno: wx.getStorageSync("workNo"),
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.state == 1) {
          _this.setData({
            unreadNum: res.data.data.unreadCount
          })
        }
      }
    })
  }

})